<%@ page language="java" import="java.util.*" pageEncoding="utf-8" isELIgnored="false"%>
<script type="text/javascript">
	var admin_lb_datagrid;
	var admin_lb_editRow = undefined;
	$(function() {
		admin_lb_datagrid = $('#admin_lb_datagrid').datagrid({
			url : '${pageContext.request.contextPath}/lbAction!dataGrid.action',
			fit : true,
			fitColumns : false,
			border : false,
			striped:true,
			rownumbers: true,//显示条数
			idField : 'id',
			sortOrder : 'asc',
			/*pagePosition : 'both',*/
			columns : [ [ {
				field : 'id',
				title : '状态编码',
				width : 150,
				checkbox: true//将显示的方式为checkbox 多选
			},  {
				field : 'lb',
				title : '状态名称',
				width : 250,
				editor : 'text'//将显示的方式为文本形式
			}] ],
			toolbar : [ {
				text : '增加',
				iconCls : 'icon-add',
				handler : function() {
					if(!admin_lb_savePrompt()){
						$.messager.confirm('提示', '当前存在未保存项目，是否保存？', function(p) {
							if (p) {
								admin_lb_datagrid.datagrid('endEdit', admin_lb_editRow);
							}
						});
					}
					else
					admin_lb_add();
				}
			}, '-', {
				text : '删除',
				iconCls : 'icon-remove',
				handler : function() {
					admin_lb_del();
				}
			}, '-', {
				text : '修改',
				iconCls : 'icon-edit',
				handler : function() {
					admin_lb_edit();
				}
			}, '-', {
				text : '保存',
				iconCls : 'icon-save',
				handler : function() {
					if (admin_lb_editRow != undefined) {
						admin_lb_datagrid.datagrid('endEdit', admin_lb_editRow);
					}
				}
			}, '-', {
				text : '取消编辑',
				iconCls : 'icon-undo',
				handler : function() {
					admin_lb_datagrid.datagrid('unselectAll');
					admin_lb_datagrid.datagrid('rejectChanges');
					admin_lb_editRow = undefined;
				}
			}, '-', {
				text : '取消选中',
				iconCls : 'icon-undo',
				handler : function() {
					admin_lb_datagrid.datagrid('unselectAll');
				}
			},'-' ],
			onAfterEdit : function(rowIndex, rowData, changes) {
				var inserted = admin_lb_datagrid.datagrid('getChanges', 'inserted');
				var updated = admin_lb_datagrid.datagrid('getChanges', 'updated');
				if (inserted.length < 1 && updated.length < 1) {
					admin_lb_editRow = undefined;
					admin_lb_datagrid.datagrid('unselectAll');
					return;
				}

				var url = '';
				if (inserted.length > 0) {
					url = '${pageContext.request.contextPath}/lbAction!add.action';
				}
				if (updated.length > 0) {
					url = '${pageContext.request.contextPath}/lbAction!edit.action';
				}

				$.ajax({
					url : url,
					type:'post',
					data : rowData,
					dataType : 'json',
					success : function(r) {
						if (r.success) {
							admin_lb_datagrid.datagrid('acceptChanges');
							$.messager.show({
								msg : r.msg,
								title : '成功'		//从右下角跳出提示框显示成功
							});
							admin_lb_editRow = undefined;
							admin_lb_datagrid.datagrid('reload');
						} else {
							/*admin_lb_datagrid.datagrid('rejectChanges');*/
							admin_lb_datagrid.datagrid('beginEdit', admin_lb_editRow);
							$.messager.alert('错误', r.msg, 'error');
						}
						admin_lb_datagrid.datagrid('unselectAll');
					}
				});

			},
			onDblClickRow : function(rowIndex, rowData) {
				if (admin_lb_editRow != undefined) {
					admin_lb_datagrid.datagrid('endEdit', admin_lb_editRow);
				}

				if (admin_lb_editRow == undefined) {
					//changeEditoradmin_lb_editRow();/*改变editor*/
					admin_lb_datagrid.datagrid('beginEdit', rowIndex);
					admin_lb_editRow = rowIndex;
					admin_lb_datagrid.datagrid('unselectAll');
				}
			},
			onRowContextMenu : function(e, rowIndex, rowData) {
				e.preventDefault();
				$(this).datagrid('unselectAll');
				$(this).datagrid('selectRow', rowIndex);
				$('#menu').menu('show', {
					left : e.pageX,
					top : e.pageY
				});
			}
		});
	});
	
		function admin_lb_add() {
		if (admin_lb_editRow != undefined) {
			admin_lb_datagrid.datagrid('endEdit', admin_lb_editRow);
		}

		if (admin_lb_editRow == undefined) {
			admin_lb_datagrid.datagrid('unselectAll');
			
			admin_lb_datagrid.datagrid('insertRow', {
				index : 0,
				row : {
				
				}
			});
			admin_lb_editRow = 0;
			admin_lb_datagrid.datagrid('selectRow', 0);
			admin_lb_datagrid.datagrid('beginEdit', 0);
		}
	}
	function admin_lb_del() {
		if (admin_lb_editRow != undefined) {
			admin_lb_datagrid.datagrid('endEdit', admin_lb_editRow);
			return;
		}
		var rows = admin_lb_datagrid.datagrid('getSelections');
		var ids = [];
		if (rows.length > 0) {
			$.messager.confirm('请确认', '您要删除当前所选项目？', function(r) {
				if (r) {
					for ( var i = 0; i < rows.length; i++) {
						ids.push(rows[i].id);//将选中的ID值赋值给ids 用逗号分隔 ---
					}
					$.ajax({
						url : '${pageContext.request.contextPath}/lbAction!delete.action',
						data : {
							ids : ids.join(',')
						},
						dataType : 'json',
						success : function(r) {
							if (r.success) {
									admin_lb_datagrid.datagrid('acceptChanges');
									$.messager.show({
										msg : r.msg,
										title : '成功'
									});
									admin_lb_editRow = undefined;
									admin_lb_datagrid.datagrid('clearSelections');
									admin_lb_datagrid.datagrid('load');
								} else {
									admin_lb_datagrid.datagrid('rejectChanges');
									$.messager.alert('错误', r.msg, 'error');
								}
						}
					});
				}
			});
		} else {
			$.messager.alert('提示', '请选择要删除的记录！', 'error');
		}
	}
	function admin_lb_edit() {
		var rows = admin_lb_datagrid.datagrid('getSelections');
		if (rows.length == 1) {
			if (admin_lb_editRow != undefined) {
				admin_lb_datagrid.datagrid('endEdit', admin_lb_editRow);
				admin_lb_editRow = admin_lb_datagrid.datagrid('getRowIndex', rows[0]);
				admin_lb_datagrid.datagrid('beginEdit', admin_lb_editRow);
				admin_lb_datagrid.datagrid('unselectAll');
			}

			if (admin_lb_editRow == undefined) {
				//changeEditoradmin_lb_editRow();/*改变editor*/
				admin_lb_editRow = admin_lb_datagrid.datagrid('getRowIndex', rows[0]);
				admin_lb_datagrid.datagrid('beginEdit', admin_lb_editRow);
				admin_lb_datagrid.datagrid('unselectAll');
			}
		} else {
			$.messager.show({
				msg : '请选择一项进行修改！',
				title : '错误'
			});
		}
	}
	function admin_lb_save() {
		var inserted = admin_lb_datagrid.datagrid('getChanges', 'inserted');
		var updated = admin_lb_datagrid.datagrid('getChanges', 'updated');
		if (inserted.length < 1 && updated.length < 1) {
			admin_lb_editRow = undefined;
			admin_lb_datagrid.datagrid('unselectAll');
			return;
		}

		var url = '';
		if (inserted.length > 0) {
			url = 'lbAction!add.action';
		}
		if (updated.length > 0) {
			url = 'lbAction!edit.action';
		}
		var rowData = admin_lb_datagrid.datagrid('getData');
		console.info(rowData);

		$.ajax({
			url : url,
			type:'post',
			data : rowData,
			dataType : 'json',
			success : function(r) {
				if (r.success) {
					admin_lb_datagrid.datagrid('acceptChanges');
					$.messager.show({
						msg : r.msg,
						title : '成功'
					});
					admin_lb_editRow = undefined;
					admin_lb_datagrid.datagrid('reload');
				} else {
					/*admin_lb_datagrid.datagrid('rejectChanges');*/
					admin_lb_datagrid.datagrid('beginEdit', admin_lb_editRow);
					$.messager.alert('错误', r.msg, 'error');
				}
				admin_lb_datagrid.datagrid('unselectAll');
			}
		});
	}
	
	function admin_lb_savePrompt(){
		var inserted = admin_lb_datagrid.datagrid('getChanges', 'inserted');
		var updated = admin_lb_datagrid.datagrid('getChanges', 'updated');
		if (inserted.length < 1 && updated.length < 1) {
			return true;
		}
		return false;
	}
</script>

<div id="alluser_layout" class="easyui-layout" data-options="fit:true,border:false">
	<div data-options="region:'north'">
	</div>
	<div data-options="region:'center'">
		<table id="admin_lb_datagrid"></table>
	</div>
</div>
	

